package servlet.client;


import dao.domain.Client;
import service.ClientService;
import service.UserService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author huangzhiyang
 */
@WebServlet(name = "ClientRegisterServlet", value = "/ClientRegisterServlet")
public class ClientRegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
//  顾客注册
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String user = "client";
        String msg = "{\"msg\":\"fail\"}";
        Client client = new Client(username, name, password, 0);
        try {
            if (UserService.selectByAccount(user, username) == null) {
                System.out.println("no client");
                ClientService.store(client);
                msg = "{\"msg\":\"success\"}";
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        response.getWriter().write(msg);
        System.out.println("finish");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        System.out.println("ClientRegisterServlet现在来了一个post请求，将要去走get的代码了。");
        doGet(request, response);
    }
}
